<?php
// 应用公共文件


// 重定向失败，重新定义的函数
function redirect_test($url, $time = 0)
{
    return header("refresh:{$time};url={$url}");
}

// 数据不正确，提示并返回至指定页面
function redirect_index($title, $url, $url_status = true)
{
    $html = "<script> alert('";
    $html .= $title;

    if ($url_status) {
        $html .= "');window.location.href = '";
        $html .= $url;
        $html .= "';</script>";
    } else {
        $html .= "');window.history.go(-1);</script>";
    }
    return $html;
}

// 分隔符 数据解析
function parse_string_to_array($str)
{
    return explode(config('ams_config.separator'), $str);
}

// 角色状态解析
function par_role_status(int $status)
{
    return config('ams_config.role_status')[$status - 1]['name'];
}

// 权限状态解析
function par_permissions_status(int $status)
{
    return config('ams_config.permissions_status')[$status - 1]['name'];
}

// excel 日期转换
function excelTime($date, $time = false)
{
    if (function_exists('GregorianToJD')) {
        if (is_numeric($date)) {
            $jd = GregorianToJD(1, 1, 1970);
            $gregorian = JDToGregorian($jd + intval($date) - 25569);
            $date = explode('/', $gregorian);
            $date_str = str_pad($date[2], 4, '0', STR_PAD_LEFT) . "-" . str_pad($date[0], 2, '0', STR_PAD_LEFT) . "-" . str_pad($date[1], 2, '0', STR_PAD_LEFT) . ($time ? " 00:00:00" : '');
            return $date_str;
        }
    } else {
        $date = $date > 25568 ? $date  : 25569;
        /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
        $ofs = (70 * 365 + 17 + 2) * 86400;
        $date = date("Y-m-d", ($date * 86400) - $ofs) . ($time ? " 00:00:00" : '');
    }
    return $date;
}

// 成果分类的状态置换
function classify_edit_status(int $status)
{
    if ($status == 0 || $status == -1) {
        return '创建模板';
    }
    if ($status == 1) {
        return '重新创建模板';
    }
}

// 成果提交状态置换
function achievement_submit_status(int $status)
{
    switch ($status) {
        case 0:
            return "提交";
            break;
        case 1:
            return "已提交";
            break;
        case 2:
            return "已审核";
            break;
        case 3:
            return "已驳回";
            break;
        case 4:
            return "已通过";
            break;
    }
}

// 置顶资讯显示转换
function notice_is_top_status(int $is_top){
    if($is_top == 1){
        return "已置顶";
    }else{
        return "未置顶";
    }
}

// 是否前台显示转换
function notice_display_status(int $display){
    if($display == 1){
        return "已显示";
    }else{
        return "未显示";
    }
}